package com.bana.fvs.app.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.bana.fvs.R;
import com.bana.fvs.app.common.adapter.CommonAdapter;
import com.bana.fvs.app.common.adapter.ViewHolder;
import com.bana.fvs.app.main.entity.FogZoneListItemBean;

import java.util.List;

public class SpinerPopWindow extends PopupWindow {
    private LayoutInflater inflater;
    private ListView mListView;
    private List<FogZoneListItemBean> list;

    public SpinerPopWindow(Context context, List<FogZoneListItemBean> list) {
        super(context);
        inflater = LayoutInflater.from(context);
        this.list = list;
        init();
    }

    private void init() {
        View view = inflater.inflate(R.layout.spiner_window_layout, null);
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00);
        setBackgroundDrawable(dw);
        setOutsideTouchable(true);
        mListView = (ListView) view.findViewById(R.id.listview);
        settings();
    }

    private void settings() {
        mListView.setAdapter(new CommonAdapter<FogZoneListItemBean>(list, R.layout.spiner_item_layout) {
            @Override
            public void convert(ViewHolder holder, FogZoneListItemBean item) {
                holder.setText(R.id.tv_name, item.getWqmc());
            }
        });
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            if (mListener != null) {
                mListener.onItemClick(position);
                dismiss();
            }
        });
        //dismiss监听
        this.setOnDismissListener(() -> {
            if (mListener != null) {
                mListener.onDismiss();
                dismiss();
            }
        });
    }


    public interface OnItemClickListener {
        void onItemClick(int position);

        void onDismiss();
    }

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }
}